/**
 * 帐户的相关操作
 * 周捷
 * 2014-1-6
 */
package com.passkeeper.action;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Namespace;
import org.apache.struts2.convention.annotation.Result;
import org.apache.struts2.convention.annotation.Results;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

import com.opensymphony.xwork2.ActionSupport;
import com.passkeeper.jdbc.model.Account;
import com.passkeeper.jdbc.model.User;
import com.passkeeper.jdbc.service.inteface.IAccountService;
import com.passkeeper.math.Code;
import com.passkeeper.util.UtilFunction;

@SuppressWarnings("serial")
@Component("accountAction") 
@Scope("prototype")
@Namespace("/account")
@Results({
	@Result(name = "add", location = "/account/add.jsp"),
	@Result(name = "save", location = "/account/list.action", type="redirect"),
	@Result(name = "list", location = "/account/list.jsp"),
	@Result(name = "error", location = "/util/error.jsp") })
public class AccountAction extends ActionSupport{
	@Autowired
	private IAccountService accountService;
	
	private Account account;
	
	public Account getAccount() {
		return account;
	}

	public void setAccount(Account account) {
		this.account = account;
	}

	@Action("list")
	public String list() {
		HttpServletRequest request = ServletActionContext.getRequest();
		try {
			String type = UtilFunction.doNull(request.getParameter("type"));
			String name = UtilFunction.doNull(request.getParameter("name"));
			User user = (User) request.getSession().getAttribute("userinfo");
			request.setAttribute("list",
					accountService.getAccountListByUser(user.getId(), type, name));
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("errorMsg", e.getMessage());
			return "error";
		}
		
		return "list";
	}
	
	@Action("add")
	public String add(){
		//HttpServletRequest request = ServletActionContext.getRequest();
		return "add";
	}
	@Action("save")
	public String save(){
		HttpServletRequest request = ServletActionContext.getRequest();
		account.setUserid(((User)request.getSession().getAttribute("userinfo")).getId());
		account.setPassword(Code.CODE(account.getPassword(), request.getParameter("code")));
		accountService.insert(account);
		return "save";
	}

}
